<?php
session_start();
require_once( 'employee_db.php' );
 
$type = new M_Employee();
$user_id = $_SESSION['userid'];
$Salary_id = 1;
$Tax_ID=1;
$Fund_ID=2;
if (isset($_POST['m'])  &&  $_POST['m']!=""){

    if ($_POST['m']== 'load'){
      $code = $_POST['code'];
	  $name = $_POST['name'];
	  $status = $_POST['status'];
	  $depart = $_POST['depart'];
	  $month = $_POST['month'];
      $result=$type->loadData($code,$name,$depart,$status,$month);
      $rows=$type->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
            while($row = $result->FetchRow()){
               $data[] = array(0,$row['id']
								,$row['code']
								,$row['title'] .' '. $row['name']
								,$row['departmentname']
								,str_replace(",","<br/>",$row['telephone'])
								,$row['statusname']
								,$row['update_date']
								,$row['username']
							 ) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
      
    }else if ($_POST['m']=='detail'){
		$id = $_POST['id'];
		$month=$_POST['month'];
		
		$detail = $type->loadDataDetail($id,$month);
		
		$income = $type->loadIncome($id, $month);
		if ($type->getRowCount()>0){
			$inAr=array();
			 while($in_row = $income->FetchRow()){
               $inAr[] = array(0,$in_row['o_id']
								,$in_row['emp_salary_id']
								,$in_row["name"]
								,number_format($in_row["amount"],2,'.',',')
								,$in_row['delete_flage']
							 ) ;
            }
		}else{
			$inAr=array();
		}
		
		$Outcome = $type->loadOutcome($id, $month);
		if ($type->getRowCount()>0){
			$outAr=array();
			 while($out_row = $Outcome->FetchRow()){
               $outAr[] = array(0,$out_row['o_id']
								,$out_row['emp_salary_id']
								,$out_row["name"]
								,number_format($out_row["amount"],2,'.',',')
							 ) ;
            }
		}else{
			$outAr=array();
		}
		
		$arr= array('detail'=>$detail
					,'in'=>$inAr
					,'out'=>$outAr
					) ;
		
				 
		
		  echo json_encode($arr);
	}
	else if ($_POST['m']=='add'){
        $id=$_POST['id'];
		$list=$_POST['list'];
		$month=$_POST['month'];
		
		$ar = array();
		list($m,$y) = explode('/', $month); 
		$ar['employee_id'] = $id;
		$ar['salary']=0;
		$ar['total_in']=0;
		$ar['total_out']=0;
		$ar['month'] = $y.'/'.$m.'/01';
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		
		$emp_salary_id = $type->InsertEmployeeSalary($ar);	
		$_iSalary=0;
		$_iIn=0;
		$_iOut=0;
		
		$type->DeleteSalaryDetail($emp_salary_id);
		
		foreach ($list as $val){
			$val['emp_salary_id'] = $emp_salary_id ;
			 
			if ($val['type']=='D'){
				if ($val['o_id'] == $Tax_ID){
					$_tax= $val['amount'];
				}else if ($val['o_id'] == $Fund_ID){
					$_fund=  $val['amount'];
				}else{
					$_iOut+= $val['amount'];
				}
			}else {
				if ($val['o_id']==$Salary_id )
					$_iSalary=$val['amount'];
				else 
					$_iIn += $val['amount'];
			}	
			$type->InsertSalaryDetail($val);
		}
		
		$type->updateEmployeeSalary($emp_salary_id, $_iSalary,$_tax, $_fund,$_iIn,$_iOut,$user_id);
    } 
    else if ($_POST['m']=='update'){
		$id=$_POST['id'];
		$list=$_POST['list'];
		$month=$_POST['month'];
		$emp_salary_id=$_POST['emp_id'];
		
		$_iSalary=0;
		$_iIn=0;
		$_iOut=0;
		$_tax=0;
		$_fund=0;
		
		
		$type->DeleteSalaryDetail($emp_salary_id);
		
		foreach ($list as $val){
			$val['emp_salary_id'] = $emp_salary_id ;
			 
			if ($val['type']=='D'){
				if ($val['o_id'] == $Tax_ID){
					$_tax= $val['amount'];
				}else if ($val['o_id'] == $Fund_ID){
					$_fund=  $val['amount'];
				}else{
					$_iOut+= $val['amount'];
				}
			}else {
				if ($val['o_id']==$Salary_id )
					$_iSalary=$val['amount'];
				else 
					$_iIn += $val['amount'];
			}	
			$type->InsertSalaryDetail($val);
		}
		
		$type->updateEmployeeSalary($emp_salary_id, $_iSalary,$_tax, $_fund,$_iIn,$_iOut,$user_id);
		
    }else if ($_POST['m']=='update_salary'){
		$type->AddSalary($user_id);
		echo 1;
	} 
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if($_GET['m']=='gy')
			echo json_encode( $type->getUserStatus());
		else if($_GET['m']=='g')
			echo json_encode( $type->getUserStatus('Y'));
		else if ($_GET['m']=='d')
			echo json_encode( $type->getDepartment());
		else if ($_GET['m']=='dy')
			echo json_encode( $type->getDepartment('Y'));
		else if ($_GET['m']=='in')
			echo json_encode( $type->getIncome());
		else if ($_GET['m']=='out')
			echo json_encode( $type->getOutcome());
		else if ($_GET['m']=='m')
			echo json_encode( $type->loadMonth());
		else if ($_GET['m']=='curr_month'){
			$result = $type->getMonth();
			echo  $result['value'];
		}
		else if ($_GET['m']=='next_month'){
			$result = $type->getNextMonth();
			echo  $result['value'];
		}
	}
}
 
?>